/**
 * Created by dmrj on 2016/5/6.
 */
define(['jquery', 'appConst', 'appMessageBox', 'model/global', 'datetimepicker/datetimepicker'], function ($, appConstant, msgBox, global) {
    var data = {
        searchFields: {}
    };
    var components = {};
    var that;
    return {
        init: function () {
            that = this;
            components = {
                txt_search_content: $('#txt_search_content'),
                txt_search_operation: $('#txt_search_operation'),
                txt_search_actor: $('#txt_search_actor'),
                txt_search_startTime: $('#txt_search_startTime'),
                txt_search_endTime: $('#txt_search_endTime'),
                dt_logs: $('#dt_logs'),
                btn_search: $('#btn_search')
            };
            that.initSearchBar();
            that.initLogTable();
        },
        initSearchBar: function () {
            components.txt_search_startTime.datetimepicker({
                format: $.i18n.prop('APP_DATE_FORMAT_TIMEPICKER'),
                language: 'zh_CN',
                autoclose: true,
                todayBtn: true,
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>'
            });

            components.txt_search_startTime.on('change', function () {
                var date = components.txt_search_startTime.datetimepicker('getDate');
                var stopTime = components.txt_search_endTime.datetimepicker('getDate');
                if (date > stopTime) {
                    components.txt_search_endTime.val('');
                }
                components.txt_search_endTime.datetimepicker('setStartDate', date);
            });
            components.txt_search_endTime.datetimepicker({
                format: $.i18n.prop('APP_DATE_FORMAT_TIMEPICKER'),
                language: 'zh_CN',
                autoclose: true,
                todayBtn: true,
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>'
            });
            components.txt_search_endTime.on('change', function () {
                var date = components.txt_search_startTime.datetimepicker('getDate');
                var stopTime = components.txt_search_endTime.datetimepicker('getDate');
                if (date > stopTime) {
                    components.txt_search_startTime.val('');
                    components.txt_search_endTime.datetimepicker('setStartDate', null);
                }
            });

            components.btn_search.on("click", function () {

                components.dt_logs.DataTable().ajax.reload();
            });
        },
        initLogTable: function () {
            components.dt_logs.dataTable({
                "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'><'col-sm-6 col-xs-12 hidden-xs'>>r" +
                "t" +
                "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
                "processing": true,
                "pageLength": global.getPageSize(),
                "paging": true,
                "autoWidth": false,
                "serverSide": true,
                "language": appConstant.table_language(),
                "ordering": false,
                "ajax": {
                    "url": 'system/log/list',
                    "type": "POST",
                    "dataSrc": "body",
                    "traditional": true,
                    "data": that.getSearchCondition,
                    "error": function (e) {
                        if (e.status == 401) {
                            msgBox.sessionFailureBox();
                        }
                        if (e.status == 500) {
                            msgBox.serverFailureBox();
                        }
                        if (e.status == 404) {
                            msgBox.pathFailureBox();
                        }
                    }
                },
                "columns": [
                    {"data": null},
                    {"data": "id"},
                    {"data": "createTime"},
                    {"data": "createBy"},
                    {"data": "content"},
                    {"data": "operation"},
                    {"data": "result"}

                ],
                "columnDefs": [
                    {
                        "targets": [1],
                        "visible": false
                    },
                    {
                        "targets": [2],
                        "render": function (d, type, full) {
                            var time = new Date(full.createTime);
                            return time.Format($.i18n.prop("APP_DATE_FORMAT_FULL"));
                        }
                    },
                    {
                        "targets": [6],
                        "render": function (d, type, full) {
                            var str = "";
                            if (d == "Success") {
                                str = "<span class='label label-success'>" + $.i18n.prop("LOG_SUCCESS") + "</span>";
                            } else if (d == "Failure") {
                                str = "<span class='label label-danger'>" + $.i18n.prop("LOG_FAILURE") + "</span>";
                            }
                            return str;
                        }
                    }

                ],
                initComplete: function (settings, json) {
                    $("[rel=tooltip]").tooltip();
                }
            });
            /*Deal with the index number of column 1;*/
            components.dt_logs.DataTable().on('draw.dt', function () {
                var dt = $(this).dataTable();
                dt.api().column(0, {draw: 'applied'}).nodes().each(function (cell, i) {
                    var pageNum = dt.api().page();
                    var pageSize = dt.api().page.len();
                    cell.innerHTML = pageNum * pageSize + i + 1;
                });
            });


        },
        getSearchCondition: function (params) {
            var searchParam = {
                'search_content': components.txt_search_content.val(),
                'search_operation': components.txt_search_operation.val(),
                'search_startTime': components.txt_search_startTime.val(),
                'search_endTime': components.txt_search_endTime.val(),
                'search_actor': components.txt_search_actor.val()
            };
            data.searchFields = searchParam;

            $.extend(params, searchParam);
            params['draw'] = params.draw;

            return params;
        }
    }
});